<template>
  <div class="app-container">
    <el-card shadow="never" v-loading="loading">
      <el-row :gutter="5">
        <el-col :span="24" class="main-card-view">
          <el-row>
            <el-card shadow="never" class="top-search-view-card">
              <el-row>
                <el-form :inline="true" :model="searchForm" class="demo-form-inline search-view">
                  <el-form-item label="用户昵称">
                    <el-input v-model="searchForm.nickname" placeholder="请输入昵称"></el-input>
                  </el-form-item>
                  <el-form-item label="手机号">
                    <el-input type='number' v-model="searchForm.tel" placeholder="请输入手机号"></el-input>
                  </el-form-item>
                  <el-form-item>
                    <el-button type="primary" @click="handleSearch" class="search-button">查询</el-button>
                  </el-form-item>
                  <el-form-item>
                    <el-button type="primary" @click="excExport" class="search-button">导出</el-button>
                  </el-form-item>
                </el-form>
              </el-row>
            </el-card>
          </el-row>

          <el-row>
            <el-card shadow="never">
              <el-table :data="tableData" :stripe="true" style="width: 100%">
                <el-table-column prop="avatar" align="center" label="头像">
                  <template slot-scope="scope">
                    <el-image style="width: 50px;height: 50px;" :src="scope.row.avatar"/>
                  </template>
                </el-table-column>
                <el-table-column prop="nickname" align="center" label="用户昵称" />
                <el-table-column prop="tel" align="center" label="手机号" />
                <!-- <el-table-column prop="status" align="center" label="状态" :formatter="healthStatus"/> -->
               <el-table-column prop="updateTime" align="center" label="最新登录时间" />
               <el-table-column prop="createTime" align="center" label="注册时间" />
                <el-table-column align="center" label="操作" width="200">
                    <template slot-scope="scope">
                        <el-button type="text" class="table-delete-btn"
                                   @click="handleDelete(scope.$index, scope.row)"></el-button><!--删除-->
                    </template>
                </el-table-column>
              </el-table>
              <XPagination ref="pageInfo" :total="total" :get-data="getList" />
            </el-card>
          </el-row>
        </el-col>
      </el-row>
    </el-card>

  </div>
</template>

<script>
import XPagination from "../../components/xpagination/XPagination";
import * as member from "../../api/member"
import XLSX from 'xlsx';
export default {
  name: "list",
  components: { XPagination },
  data () {
    return {
      searchForm: {
        nickname: ""
      },
      tableData: [],
      loading: false,
      dialogVisible: false,
      total: 0,
    }
  },
  methods: {
    getList (pageInfo) {
      const param = {
        ...this.searchForm,
        ...pageInfo
      }
      member.queryByPage(param).then(response => {
        console.log(response)
        this.tableData = response.data.data.rows
        this.total = response.data.data.total
        this.loading = false
      }).catch(error => {
        console.log(error)
      })
    },
    handleDelete(index, data) {
        this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            this.loading = true
            member.deleteById(data.uid).then(result => {
                this.loading = false
                this.$message.success(result.data.msg)
                this.handleSearch()
            }).catch(error => {
                this.loading = false
                console.log(error)
            })
        }).catch(() => {
            console.log('取消删除')
        });
    },
    // excExport(){
    //     let sheetData = []
    //     sheetData = this.tableData.map(item => ({
    //         '图像': item.avatar,
    //         '用户昵称': item.nickname,
    //         '手机号': item.tel,
    //         '最新登录时间': item.upateTime,
    //         '注册时间': item.createTime
    //     }))
    //     const wb = XLSX.utils.book_new();
    //     const ws = XLSX.utils.json_to_sheet(sheetData);
    //     XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');
    //     XLSX.writeFile(wb, 'user_data.xlsx');
    // },
    excExport(){
        let sheetData = []
        sheetData = this.tableData.map(item => ({
            '用户昵称': item.nickname,
            '手机号': item.tel,
            '最新登录时间': item.upateTime,
            '注册时间': item.createTime
        }))
        const wb = XLSX.utils.book_new();
        const ws = XLSX.utils.json_to_sheet(sheetData);
        XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');
        XLSX.writeFile(wb, 'user_data.xlsx');
    },
    handleSearch () {
      this.$refs.pageInfo.loadData()
    },
    healthStatus(row,column,cellValue,index){
        switch(cellValue){
            case 1:
                return '正常'
            case 2:
                return '冻结'
        }
    }
  },


  mounted () {
    this.handleSearch()
  }
}
</script>

<style scoped lang="scss"></style>
